package com.ruoyi.qiushi.service.user.impl;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qiushi.domain.system.QiushiInstitution;
import com.ruoyi.qiushi.domain.user.MemberFamily;
import com.ruoyi.qiushi.mapper.system.QiushiInstitutionMapper;
import com.ruoyi.qiushi.mapper.user.MemberFamilyMapper;
import com.ruoyi.qiushi.service.user.IMemberFamilyService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户家人Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
public class MemberFamilyServiceImpl implements IMemberFamilyService {
    @Autowired
    private MemberFamilyMapper memberFamilyMapper;
    @Autowired
    private QiushiInstitutionMapper qiushiInstitutionMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询用户家人
     *
     * @param id 用户家人主键
     * @return 用户家人
     */
    @Override
    public MemberFamily selectMemberFamilyById(Long id) {
        return memberFamilyMapper.selectMemberFamilyById(id);
    }

    /**
     * 查询用户家人列表
     *
     * @param memberFamily 用户家人
     * @return 用户家人
     */
    @Override
    public List<MemberFamily> selectMemberFamilyList(MemberFamily memberFamily) {
        List<MemberFamily> memberFamilyList = new ArrayList<>();

        Long deptId = SecurityUtils.getDeptId(); // 获取部门ID

        if (deptId == 103L) {
            memberFamilyList = memberFamilyMapper.selectMemberFamilyList(new MemberFamily());
        } else {
            SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
            String[] ancestors = sysDept.getAncestors().split(",");
            if (ancestors.length > 1) deptId = Long.parseLong(ancestors[1]);

            Deque<Long> deque = new LinkedList<>();
            deque.addFirst(deptId);

            while (!deque.isEmpty()) {
                Long id = deque.removeLast();
                List<MemberFamily> temp = memberFamilyMapper.selectMemberFamilyByInstitutionId(id);
                memberFamilyList.addAll(temp);

                List<QiushiInstitution> list = qiushiInstitutionMapper.selectQiushiInstitutionByParentId(id);
                for (QiushiInstitution l : list) deque.addFirst(l.getId());
            }
        }

        return memberFamilyList;
    }

    /**
     * 新增用户家人
     *
     * @param memberFamily 用户家人
     * @return 结果
     */
    @Override
    public int insertMemberFamily(MemberFamily memberFamily) {
        memberFamily.setCreateTime(DateUtils.getNowDate());
        return memberFamilyMapper.insertMemberFamily(memberFamily);
    }

    /**
     * 修改用户家人
     *
     * @param memberFamily 用户家人
     * @return 结果
     */
    @Override
    public int updateMemberFamily(MemberFamily memberFamily) {
        memberFamily.setUpdateTime(DateUtils.getNowDate());
        return memberFamilyMapper.updateMemberFamily(memberFamily);
    }

    /**
     * 删除用户家人信息
     *
     * @param id 用户家人主键
     * @return 结果
     */
    @Override
    public int deleteMemberFamilyById(Long id) {
        return memberFamilyMapper.deleteMemberFamilyById(id);
    }
}
